package com.example.xianji.adapter;

import java.io.File;
import java.util.ArrayList;

import com.example.xianji.R;
import com.example.xianji.bean.BaiKe_Detail_DuiXiang;
import com.example.xianji.utils.DownLoadUtils;
import com.example.xianji.utils.DownLoadUtils.OnDownloadListener;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BaiKe_Detail_Adapter extends BaseAdapter {
	private Context mContext;
	private ArrayList<BaiKe_Detail_DuiXiang> list;
	private final String path;
	private int outHeight, outWidth, height;
	private DownLoadUtils downLoadUtils;
	private int width;
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 2) {

				BaiKe_Detail_DuiXiang mBaiKe_Detail_DuiXiang = (BaiKe_Detail_DuiXiang) msg.obj;

				mBaiKe_Detail_DuiXiang.getTextview().setText(
						Html.fromHtml(mBaiKe_Detail_DuiXiang.getHtml(),
								imageGetter, null));
			}
		}

	};

	public BaiKe_Detail_Adapter(Context mContext,
			ArrayList<BaiKe_Detail_DuiXiang> list) {
		super();
		this.mContext = mContext;
		this.list = list;
		WindowManager wm = (WindowManager) mContext
				.getSystemService(Context.WINDOW_SERVICE);
		width = wm.getDefaultDisplay().getWidth();
		path = mContext.getCacheDir() + "/mnt/sdcard/downimg/baike/";
		// new File(path).mkdirs();
		// downLoadUtils = new DownLoadUtils();
		/*************************** isCancle 可以关闭流，控制数据的加载避免多个线程的堵塞 ***********************************/

		// downLoadUtils.setCancle(false);
		// 初始化下载类

	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder mHolder = null;
		if (convertView == null) {
			mHolder = new Holder();
			convertView = View.inflate(mContext, R.layout.baike_detail_item,
					null);
			mHolder.text_baike_title_name = (TextView) convertView
					.findViewById(R.id.text_baike_title_name);
			mHolder.baike_detail_content = (TextView) convertView
					.findViewById(R.id.baike_detail_content);

			convertView.setTag(mHolder);
		}

		mHolder = (Holder) convertView.getTag();

		mHolder.text_baike_title_name.setText(list.get(position)
				.getDetail_name());

		setData(mHolder.baike_detail_content, list.get(position)
				.getDetail_content());

		return convertView;
	}

	class Holder {
		TextView text_baike_title_name, baike_detail_content;
	}

	final Html.ImageGetter imageGetter = new Html.ImageGetter() {

		public Drawable getDrawable(final String source) {
			Drawable drawable = null;
			String fileString = path + String.valueOf(source.hashCode());
			Log.i("sss", "kkkkkkkk");
			// 判断SD卡里面是否存在图片文件
			if (new File(fileString).exists()) {
				// downLoadUtils.setCancle(true);
				Bitmap bitmap = null;
				// 获取本地文件返回Drawable
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inJustDecodeBounds = true;// 它仅仅会把它的宽，高取回来给你，这样就不会占用太多的内存

				Bitmap bitmap1 = BitmapFactory.decodeFile(fileString, options);/* 这里返回的bmp是null */

				outHeight = options.outHeight;
				outWidth = options.outWidth;
				Log.i("wwwwwww", outWidth + "");
				if (outWidth > 600) {
					/**
					 * 需要在图片不变形的前提下得到宽度为400的缩略图。 那么我们需要先计算一下缩放之后，图片的高度是多少
					 */
					height = options.outHeight * 400 / outWidth;

					options.inSampleSize = options.outWidth / 400;
					options.outWidth = 400;
					options.outHeight = height;
					options.inJustDecodeBounds = false;
					bitmap = BitmapFactory.decodeFile(fileString, options);
					drawable = new BitmapDrawable(bitmap);
				} else {
					/*
					 * options.inJustDecodeBounds = false; bitmap =
					 * BitmapFactory.decodeFile(fileString, options);
					 */
					drawable = Drawable.createFromPath(fileString);
				}

				/*
				 * if (drawable == null) {
				 *//*************************** isCancle 可以关闭流，控制数据的加载避免多个线程的堵塞 ***********************************/
				/*
				 * Log.i("进入进入进入进入", outWidth + "");
				 * //downLoadUtils.setCancle(false);
				 * downLoadUtils.download(source, path +
				 * String.valueOf(source.hashCode()));
				 * 
				 * return drawable; }
				 */

				// 设置图片边界怎么绘制

				if (drawable != null) {

					Log.i("widthwidthwidth", drawable.getIntrinsicWidth() + "");
					if (drawable.getIntrinsicWidth() > 300) {
						drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
								drawable.getIntrinsicHeight());
					} else if (drawable.getIntrinsicWidth() < 200) {
						drawable.setBounds(0, 0,
								drawable.getIntrinsicWidth() * 3,
								drawable.getIntrinsicHeight() * 3);
					} else {
						drawable.setBounds(0, 0,
								drawable.getIntrinsicWidth() * 2,
								drawable.getIntrinsicHeight() * 2);
					}

				}

				return drawable;
			} else {
				Log.i("sss", "不存在不存在");

				downLoadUtils.download(source,
						path + String.valueOf(source.hashCode()));
				return drawable;
			}

		};

	};

	private void setData(final TextView tView, final String details) {
		new File(path).mkdirs();
		// 初始化下载类
		downLoadUtils = new DownLoadUtils();
		downLoadUtils.setCancle(false);
		// 设置下载类监听事件
		downLoadUtils.setOnDownloadListener(new OnDownloadListener() {

			// 下载进度
			public void onDownloadUpdate(DownLoadUtils manager, int percent) {
			}

			// 下载失败
			public void onDownloadError(DownLoadUtils manager, Exception e) {
			}

			// 开始下载
			public void onDownloadConnect(DownLoadUtils manager) {
			}

			// 完成下载
			public void onDownloadComplete(DownLoadUtils manager, Object result) {
				// 替换sTExt的值，就是把图片的网络路径换成本地SD卡图片路径(最早想法，可以不需要这样做了)
				// sText.replace(result.toString(),
				// path+String.valueOf(result.hashCode()));
				// 再一次赋值给Textview

				Log.i("DownloadCompleteDownloadComplete", result
						+ "DownloadCompleteDownloadCompleteDownloadComplete");
				// manager.setCancle(true);

				tView.setText(Html.fromHtml(details, imageGetter, null));
				 BaiKe_Detail_Adapter.this.notifyDataSetChanged();

			}
		});
		// 给Textview赋值

		tView.setText(Html.fromHtml(details, imageGetter, null));
	}

}
